package com.stock.stockana.ui.fragments;

import android.app.LoaderManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.stock.stockana.R;
import com.stock.stockana.beans.StockInfoBean;
import com.stock.stockana.events.StockListChangedEvent;
import com.stock.stockana.model.StockCursorLoader;
import com.stock.stockana.ui.activities.SelectStockActivity;
import com.stock.stockana.ui.activities.StockDetailActivity;
import com.stock.stockana.ui.adapter.SearchedStockAdapter;
import com.stock.stockana.ui.listener.OnStockItemClickedListener;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by ljgsonx on 2016/1/21.
 */
public class SelectStockFragment extends Fragment{

    private final static String TAG = "SelectStockFragment--->";

    private View mView;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private SearchedStockAdapter mSearchedStockAdapter;
    private LoaderManager mLoaderManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.recyclerview, container, false);
        mRecyclerView = (RecyclerView) mView.findViewById(com.stock.publiclib.R.id.id_recyclerview);
        mLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        mLoaderManager = getActivity().getLoaderManager();
        mSearchedStockAdapter = new SearchedStockAdapter(getActivity(), mLoaderManager);
        mSearchedStockAdapter.setOnItemClickedListener(new OnStockItemClickedListener() {
            @Override
            public void onStockItemCliked(StockInfoBean clickedStock) {
                Intent intent = new Intent(getActivity(), StockDetailActivity.class);
                intent.putExtra("clickedStock", clickedStock);
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left);
            }
        });
        mRecyclerView.setAdapter(mSearchedStockAdapter);
        mRecyclerView.setLayoutManager(mLayoutManager);
        return mView;
    }

    /**
     * Description: 判断本页面的股票是否为自选股,
     *      依赖于主页面中自选股列表中是否存在,因此需监听自选股列表的变化事件
     * Author: ljgsonx
     * Time: 2016/1/23 18:57
     */
    @Subscribe
    public void onEventMainThread(StockListChangedEvent event){
        ((SelectStockActivity)getActivity()).stopPopLoading();
        mSearchedStockAdapter.setRequestFinished(true);
        if (event.getEventCode() == StockListChangedEvent.EVENTCODE_ADD_FAILED) {
//            Toast.makeText(getActivity(), "添加失败!", Toast.LENGTH_SHORT).show();
        }else if(event.getEventCode() == StockListChangedEvent.EVENTCODE_CANCEL_FAILED){
//            Toast.makeText(getActivity(), "取消失败!", Toast.LENGTH_SHORT).show();
        }else{
            mSearchedStockAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mLoaderManager.destroyLoader(StockCursorLoader.ID);
        EventBus.getDefault().unregister(this);
    }

}
